package com.sanyang.lawyer.controller.audit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sanyang.lawyer.ResultApi.CodeMsg;
import com.sanyang.lawyer.ResultApi.ResponseResult;
import com.sanyang.lawyer.entity.Audit;
import com.sanyang.lawyer.entity.Case_audit;
import com.sanyang.lawyer.service.Audit.Audit_sealService;
import com.sanyang.lawyer.utils.ActivitiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("seal")
public class Audit_sealController {  //用印审核
    
    @Autowired
    private ActivitiUtils activitiUtils;
    
    private ObjectMapper objectMapper = new ObjectMapper();
    
    @Autowired
    private Audit_sealService sealService;
    
    
    @RequestMapping("start")
    public ResponseResult start(String data){
        Case_audit case_audit = null;
        try {
            case_audit = objectMapper.readValue(data, Case_audit.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        
        //1:开始流程
        String processid = activitiUtils.start("委托代理人","my05");
        case_audit.getAuditid().setAuditid(processid);
        //2：审核表添加数据 
        sealService.insertInAudit(case_audit);
        //3：中间表添加数据
        sealService.insertInCase_Audit(case_audit);
        //4：推行到经理审核
        activitiUtils.Mycomplete(processid);
        
        return ResponseResult.success("成功添加！~");
    }
    
    
    //根据流程id查询出用印审核相关的内容
    @RequestMapping("querySealByprocessid")
    public ResponseResult<Case_audit> querySealByprocessid(String processid){
        return ResponseResult.success(sealService.querySealByprocessid(processid));
    }
    
    
    //完成流程的审批
    @RequestMapping("complateSealprocess")
    public ResponseResult<String> complateSealprocess(String data){
        Audit audit = null;
        try {
            audit = objectMapper.readValue(data, Audit.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        System.out.println(audit);
        
        //1：完成流程
        activitiUtils.Mycomplete(audit.getAuditid());
        //2：给流程表添加数据
        sealService.complateUpdate(audit);
        return ResponseResult.success("完成流程");
    }
    
    
    
}
